package ssf.core.common;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;

import ssf.core.common.ExecuteResult;
import ssf.core.Constants;
import ssf.core.util.CommonUtil;
import ssf.core.util.QueryUtil;

public class CommonBaseAction extends Action {
	/* 日志 */
	protected final Log log = LogFactory.getLog(getClass());

	/* 获取request对象传递过来的非数组形参数，并封装成Map类型 */
	public static Map<String, Object> parameters = new HashMap<String, Object>();

	public static Map<String, Object> getParameters() {
		return parameters;
	}

	/* 默认返回SUCCESS */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.getSession().getServletContext().setAttribute("BASE",
				request.getContextPath());
		return mapping.findForward(Constants.RETURN_SUCCESS);
	}

	public void test() {

	}

	/* 分页信息 */
	protected int pageNum = 1;

	protected int pageSize = Constants.DEFAULT_PAGE_SIZE;

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getMaxPageSize() {
		return Constants.MAX_PAGE_SIZE;
	}

	public int getDefaultPageSize() {
		return Constants.DEFAULT_PAGE_SIZE;
	}

	/* 前台通用操作结果返回页 */
	public static final String EXECUTE_RESULT_FRONT = Constants.EXECUTE_RESULT_FRONT;

	private ExecuteResult executeResultFront;

	private ExecuteResult buildExecuteResultFront(HttpServletRequest request) {
		if (executeResultFront == null) {
			executeResultFront = new ExecuteResult();
			request.getSession().setAttribute(EXECUTE_RESULT_FRONT,
					executeResultFront);
		}
		return executeResultFront;
	}

	public void setResultFront(HttpServletRequest request, String result) {
		buildExecuteResultFront(request).setResult(result);
	}

	public void addMessageFront(HttpServletRequest request, String message) {
		buildExecuteResultFront(request).addMessage(message);
	}

	public void addRedirURLFront(HttpServletRequest request, String desc,
			String url) {
		buildExecuteResultFront(request).addRedirURL(desc, url);
	}

	public ExecuteResult getExecuteResultFront(HttpServletRequest request) {
		return (ExecuteResult) request.getSession().getAttribute(
				EXECUTE_RESULT_FRONT);
	}

	/* 后台通用操作结果返回页 */
	public static final String EXECUTE_RESULT = Constants.EXECUTE_RESULT;

	private ExecuteResult executeResult;

	private ExecuteResult buildExecuteResult(HttpServletRequest request) {
		if (executeResult == null) {
			executeResult = new ExecuteResult();
			request.getSession().setAttribute(EXECUTE_RESULT, executeResult);
		}
		return executeResult;
	}

	public void setResult(HttpServletRequest request, String result) {
		buildExecuteResult(request).setResult(result);
	}

	public void addMessage(HttpServletRequest request, String message) {
		buildExecuteResult(request).addMessage(message);
	}

	public void addRedirURL(HttpServletRequest request, String desc, String url) {
		buildExecuteResult(request).addRedirURL(desc, url);
	}

	public ExecuteResult getExecuteResult(HttpServletRequest request) {
		return (ExecuteResult) request.getSession()
				.getAttribute(EXECUTE_RESULT);
	}

	public String getQueryStringWithoutPageNum() {
		Map m = getParameters();
		m.remove("pageNum");
		return QueryUtil.getQueryString(m);
	}

	public String getFullUrlWithoutPageNum(HttpServletRequest request) {
		return request.getServletPath() + "?" + getQueryStringWithoutPageNum();
	}

	public String getQueryStringWithoutPageInfo() {
		Map m = getParameters();
		m.remove("pageNum");
		m.remove("pageSize");
		return QueryUtil.getQueryString(m);
	}

	public String getFullUrlWithoutPageInfo(HttpServletRequest request) {
		return request.getServletPath() + "?" + getQueryStringWithoutPageInfo();
	}

	/* 记录当前页面作为返回地址 */
	public void setReferUrl(HttpServletRequest request) {
		setReferUrl(request, Constants.REFER_URL_DEFAULT_KEY);
	}

	public String getReferUrl(HttpServletRequest request) {
		return getReferUrl(request, Constants.REFER_URL_DEFAULT_KEY);
	}

	public String getFullReferUrl(HttpServletRequest request) {
		return getFullReferUrl(request, Constants.REFER_URL_DEFAULT_KEY);
	}

	public void setReferUrl(HttpServletRequest request, String key) {
		Map referUrlMap = (Map) request.getSession().getAttribute(
				Constants.REFER_URL);
		if (referUrlMap == null) {
			referUrlMap = new HashMap();
		}
		String currUrl = QueryUtil.getRequestURL(request);
		referUrlMap.put(key, currUrl);
		request.getSession().setAttribute(Constants.REFER_URL, referUrlMap);
	}

	public String getReferUrl(HttpServletRequest request, String key) {
		Map referUrlMap = (Map) request.getSession().getAttribute(
				Constants.REFER_URL);
		if (referUrlMap == null) {
			return "";
		}
		return referUrlMap.get(key) == null ? "" : (String) referUrlMap
				.get(key);
	}

	public String getFullReferUrl(HttpServletRequest request, String key) {
		return request.getContextPath() + getReferUrl(request, key);
	}

	/* 工具类方法 */
	public Date getNow() {
		return new Date();
	}

	private static SimpleDateFormat fileFormatter = new SimpleDateFormat(
			"yyyyMMddHHmmssSSS");

	// 获取格式化后的日期（到毫秒）
	public String getNowString() {
		return fileFormatter.format(getNow());
	}

	public String substring(String str, int toCount, String suffix) {
		return CommonUtil.substring(str, toCount, suffix);
	}

}
